package com.lanyuan.a.service.union;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lanyuan.a.bean.Comment;
import com.lanyuan.a.bean.Order;
import com.lanyuan.a.bean.Store;
import com.lanyuan.a.bean.User;
import com.lanyuan.a.bean.Window;
import com.lanyuan.a.bean.WindowFood;
import com.lanyuan.a.bean.WindowSale;
import com.lanyuan.a.bean.union.FoodOrder;
import com.lanyuan.a.service.CommentService;
import com.lanyuan.a.service.OrderService;
import com.lanyuan.a.service.StoreService;
import com.lanyuan.a.service.WindowFoodService;
import com.lanyuan.a.service.WindowSaleService;
import com.lanyuan.a.service.WindowService;

@Service("userManageService")
public class UserManageService {
	@Autowired
	WindowSaleService windowSaleService;
	@Resource
	StoreService storeService;
	@Resource
	WindowFoodService windowFoodService;
	@Resource
	OrderService orderService;
	@Resource
	WindowService windowService;
	@Resource
	CommentService commentService;
	public List<FoodOrder> list(List<WindowFood> wsList){
		List<FoodOrder> foList = new ArrayList<FoodOrder>();
		int i = 0;
		for(WindowFood ws : wsList){
			System.out.println(ws);
			Store store = storeService.findByWFid(ws.getW_f_id());
			System.out.println(store);
			FoodOrder fo = new FoodOrder();
			fo.setWindowFoodValue(ws);
			if(store != null) fo.setStoreValue(store);
			String optionNum = fo.getOptionNum();
			optionNum = optionNum.replace("id='num'", "id='num"+i+"'");
			optionNum = optionNum.replace("id='wfid'", "id='wfid"+i+"'");
			optionNum = optionNum.replace("value='wfid'", "value='"+ws.getW_f_id()+"'");
			fo.setOptionNum(optionNum);
			String button = fo.getButton();
			button = button.replace("cc(this)", ("cc("+i+")"));
			button = button.replace("id='order'", ("id='order"+i+"'"));
			button = button.replace("id='can'", ("id='can"+i+"'"));
			button = button.replace("cancel(this)", ("cancel("+(i++)+")"));
			fo.setButton(button);
			System.out.println(fo);
			foList.add(fo);
		}
		return foList;
	}
	
	public  String addOrder(String[] data, int userid){
		
		int wfid = Integer.parseInt(data[0]);
		int num  = Integer.parseInt(data[1]);
		
		Store store = storeService.findByWFid(wfid);
		if(store.getLeft() < num){
			return "error";
		}
		Order order = new Order();
		order.setIsbuy("0");
		order.setUserId(userid);
		order.setW_f_id(wfid);
		order.setNum(num);
		Date date = new Date();
		order.setOrdertime(date);
		WindowFood wf = windowFoodService.getByWFid(wfid);
		order.setF_id(wf.getF_id());
		order.setF_name(wf.getF_name());
		order.setPrice(wf.getF_price());
		
		orderService.addOrder(order);
		return "success";
	}
	
	
	public String orderBuy(String[] orderId){
		String rt = "success";
		for(String id : orderId){
			Order order =  orderService.getById(id);
			int wfid = order.getW_f_id();
			int num = order.getNum();

			//仓库减
			Store store = storeService.findByWFid(wfid);
			if(store.getLeft() < num){
				rt = "error";
				continue;
			}
			store.setLeft(store.getLeft() - num);
			storeService.update(store);
			
			//食品销量加
			WindowFood wf = windowFoodService.getByWFid(wfid);
			wf.setF_salenum(wf.getF_salenum() + num);
			windowFoodService.updat(wf);
			
			//窗口销售加一条记录
			WindowSale ws = new WindowSale();
			ws.setF_salenum(num);
			ws.setWindowFoodValue(wf);
			windowSaleService.addSale(ws);
			
			//订单改为已购买
			order.setIsbuy("1");
			order.setOrdertime(new Date());
			orderService.addOrder(order);
		}
		System.out.println("oooooooooooo");
		return rt;
	}
	
	public void saveComment(String wid , String content, User user){
		Comment c = new Comment();
		Window w = windowService.get(Integer.parseInt(wid));
		if(w == null) return ;
		c.setW_id(Integer.parseInt(wid));
		c.setW_name(w.getW_name());
		c.setU_id(user.getId());
		c.setU_name(user.getUsername());
		c.setContent(content);
		c.setCreatetime(new Date());
		c.setIsdel("0");
		
		commentService.saveComment(c);
	}
}
